<?php
namespace app\admin\controller;
use think\Db;
use think\facade\{
    View,Env,Config
};
class Publics extends Base
{
    //登录
    public function login(){
        if($this->userinfo) r(url('index/index'));
        if(is_post()){
            $d = request()->post();
            $rule = [
                'username'=>'require',
                'password'=>'require',
                'code'=>'require',
            ];
            $msg = [];
            $r = $this->validate($d, $rule,$msg);
            if($r!==true) return err($r);

            if(!captcha_check($d['code'])){
                session('js.name','code');
                return err('验证码错误!');
            }
            $rs = model('AdminUser')->login($d['username'], $d['password']);
            if($rs){
                session('js.url',url('index/index'));
                session('js.msg','登录成功!');
                return suc();
            }
            return err(model('AdminUser')->getError());
        }
        return $this->fetch('',[],['layout_on'=>false]);
    }
    //退出
    public function logout(){
        model('adminUser')::where('token',session('token'))->update(['token'=>'']);
        session(null);
        cookie(null);
        r(url('publics/login'));
    }
    //后台菜单配置
    public function menudatas(){
        if(session("?arr_id")){
            $arr_id = session("arr_id");
        }else{
            $menu = c('adminmenu.');
            $arr_id = admin_menu_arr($menu,0);
        }
        $rs     = admin_menu_tree($arr_id,0);
        return suc($rs);
    }
}
